package com.zengqingfa.thread.demo1;

import java.util.concurrent.*;

/**
 * @fileName: CreateThread
 * @author: zengqf3
 * @date: 2020-10-7 11:24
 * @description:
 */
public class CreateThread3 implements Callable<String> {

    @Override
    public String call() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Callable 创建线程";
    }

    public static void main(String[] args) throws Exception {
        //调用方式一
        CreateThread3 ct1 = new CreateThread3();
        FutureTask<String> ft = new FutureTask<>(ct1);
        new Thread(ft).start();
        //此方法为阻塞方法，直到拿到返回值
        String result = ft.get();
        System.out.println("方式一：获取结果 " + result);
        //调用方式二
        //创建执行服务
        ExecutorService service= Executors.newFixedThreadPool(3);
        CreateThread3 ct2 = new CreateThread3();
        Future<String> submit = service.submit(ct2);
        System.out.println("方式二：获取结果 "+submit.get());
    }
}
